package com.achen.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * 不交给spring工厂管理的类的创建
 *
 * @author achen
 * @create 2020-04-30 9:51
 */
@Configuration //需要将这个类交给工厂管理
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //根据名称获取指定bean
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    //根据类型获取指定bean
    public static Object getBean(Class clazz) {
        return applicationContext.getBean(clazz);
    }

    //根据名称和类型获取指定bean
    public static Object getBean(String name, Class clazz) {
        return applicationContext.getBean(name, clazz);
    }
}
